package unicamp.mc;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import container_classes.Place;
import de.android1.overlaymanager.ManagedOverlay;
import de.android1.overlaymanager.ManagedOverlayGestureDetector;
import de.android1.overlaymanager.ManagedOverlayItem;
import de.android1.overlaymanager.MarkerRenderer;
import de.android1.overlaymanager.OverlayManager;
import de.android1.overlaymanager.ZoomEvent;

public class MainOnActivity extends MapActivity {
	private Button btFav,
		   		   btSearch,
		   		   btHelp;
	
	MapView mapView;
	MapController mapController;

	OverlayManager overlayManager;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_on);
        
        btFav = (Button) findViewById(R.id.btFav);
        btFav.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				favClick(); 
			}
		});
        
        btSearch = (Button) findViewById(R.id.btBusca);
        btSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				searchClick(); 
			}
		});
        
        btHelp = (Button) findViewById(R.id.btHelp);
        btHelp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				helpClick(); 
			}
		});
        
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();

		overlayManager = new OverlayManager(getApplication(), mapView);
	}
	
	protected void favClick() {
		// TODO Auto-generated method stub
		
	}
	
	private void helpClick(){
		Intent i = new Intent();
		i.setClass(this, HelpActivity.class);
		startActivity(i);
	}

	public void onWindowFocusChanged(boolean b) {
		createOverlayWithListener();
	}
	
	public void createSimpleOverlay() {

		ManagedOverlay managedOverlay = overlayManager.createOverlay("simpleOverlay");
		for (int i = 0; i < 30; i = i + 5) {
			managedOverlay.createItem(GeoHelper.geopoint[i], "Item" + i);
		}
		overlayManager.populate();
	}
	
	
	public void createOverlayWithListener() {
        //This time we use our own marker
		ManagedOverlay managedOverlay = overlayManager.createOverlay("listenerOverlay", getResources().getDrawable(R.drawable.pin_a));
		managedOverlay.createItem(GeoHelper.geopoint[1], "Item 1");
		managedOverlay.createItem(GeoHelper.geopoint[2], "Item 2");
		managedOverlay.createItem(GeoHelper.geopoint[3], "Item 3");
		
		managedOverlay.setOnOverlayGestureListener(new ManagedOverlayGestureDetector.OnOverlayGestureListener() {
			@Override
			public boolean onZoom(ZoomEvent zoom, ManagedOverlay overlay) {
				//Toast.makeText(getApplicationContext(), "Zoom yeah!", Toast.LENGTH_SHORT).show();
				return false;
			}

			@Override
			public boolean onDoubleTap(MotionEvent e, ManagedOverlay overlay, GeoPoint point, ManagedOverlayItem item) {					
				mapController.animateTo(point);
				addlocal();
				return true;
			}

			@Override
			public void onLongPress(MotionEvent e, ManagedOverlay overlay) {
				//Toast.makeText(getApplicationContext(), "Adicionando local...!", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onLongPressFinished(MotionEvent e, ManagedOverlay overlay, GeoPoint point, ManagedOverlayItem item) {
				if (item != null)
					clicou(item);
				addlocal();
			}

			@Override
			public boolean onScrolled(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, ManagedOverlay overlay) {
				return false;
			}

			@Override
			public boolean onSingleTap(MotionEvent e, ManagedOverlay overlay, GeoPoint point, ManagedOverlayItem item) {
				if (item != null)
					clicou(item);
				return false;
			}
		});
		overlayManager.populate();
	}

	private void clicou(ManagedOverlayItem item) {

		Intent i = new Intent();
		i.setClass(this, LocalActivity.class);
		Bundle b = new Bundle();
		b.putInt("logado", 1); //Logado?
		b.putInt("local", item.hashCode()); //id do lugar
		i.putExtras(b); //adiciona parametro no intent
		startActivity(i);
	}
	
	
	private void addlocal() {

		Intent i = new Intent();
		i.setClass(this, AddLocalActivity.class);
		startActivity(i);
	}
	
	
	private void searchClick(){
		Intent i = new Intent();
		Bundle b = new Bundle();
		b.putInt("logado", 1); //Logado?
		i.putExtras(b); //adiciona parametro no intent
		i.setClass(this, BuscaActivity.class);
		startActivity(i);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	/*
	private void draw() {
	     
        mapOverlays = mapView.getOverlays();
       
        icon = new HelloItemizedOverlay(this.getResources().getDrawable(R.drawable.pin_a));
        
        GeoPoint point = new GeoPoint(19240000,-99120000);
        OverlayItem overlayitem = new OverlayItem(point, "werwer", "lkauru");
        
        HelloItemizedOverlay icon2 = new HelloItemizedOverlay(this.getResources().getDrawable(R.drawable.pin_a));
        OverlayItem overlayitem2 = new OverlayItem(new GeoPoint(19500000,-99000000), "ufhgsuighl", "werwer");

        
        icon.addOverlay(overlayitem);
        mapOverlays.add(icon);
        
        icon2.addOverlay(overlayitem2);
        mapOverlays.add(icon2);
        
	}*/
}
